home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 220 / 220.xpi / chrome / flashgot.jar / content / flashgot / FlashGot.h < prev    next >
Encoding:
C/C++ Source or Header  |  2010-01-24  |  9.1 KB  |  463 lines

  1. #include "stdafx.h"
  2. //#include <atlstr.h>
  3. //#include <shellapi.h>
  4. #include <sys/stat.h>
  5. #include <comdef.h>
  6. #include <atlbase.h>
  7. #include <atlcom.h>
  8. #include <objbase.h>
  9. #include <wininet.h>
  10. #include <time.h>
  11. #include <string>
  12.  
  13. #define VERSION "1.2.0.4"
  14.  
  15. #define DMS_POLL_DELAY 100
  16. #define DMS_POLL_TIMEOUT 60000
  17. enum OpType { 
  18.         OP_ONE=0, OP_SEL=1, OP_ALL=2, 
  19.         OP_QET=3,    
  20.         OP_MIN=OP_ONE, OP_MAX=OP_QET };
  21.  
  22. typedef struct _LinkInfo 
  23. {
  24.     bstr_t url;
  25.     bstr_t comment;
  26.     bstr_t cookie;
  27.     bstr_t postdata;
  28. } LinkInfo;
  29.  
  30. #define EXTRAS_COUNT 5
  31.  
  32. typedef struct _DownloadInfo 
  33. {
  34.     char *dmName;
  35.     OpType opType;
  36.     bstr_t folder;
  37.     bstr_t *rawParms;
  38.     bstr_t referer;
  39.     LinkInfo *links;
  40.     int linksCount;
  41.     bstr_t *extras;
  42. } DownloadInfo;
  43.  
  44. void fail(char *msg, int code);
  45.  
  46. class CookieManager
  47. {
  48.     private: 
  49.         
  50.         const DownloadInfo *downloadInfo;
  51.     public:
  52.         CookieManager() : downloadInfo(NULL) {}
  53.         
  54.         CookieManager(const DownloadInfo *downloadInfo) : downloadInfo(downloadInfo)
  55.         {
  56.             int hoursToLive = atoi(downloadInfo->extras[4]);
  57.             setAll(downloadInfo, 3600 * (hoursToLive == 0 ? 1 : hoursToLive));
  58.         }
  59.  
  60.         static void makeExpiration(char *expiration, size_t buflen, int offset)
  61.         {
  62.             time_t now;
  63.             time(&now);
  64.             now += offset;
  65.             tm mytm;
  66.             if(gmtime_s(&mytm, &now) == 0)
  67.                 strftime(expiration, buflen,
  68.                     "%a, %d-%b-%Y %H:%M:%S GMT", &mytm);
  69.         }
  70.  
  71.         static void setCookie(const bstr_t *url, const bstr_t *cookie, char *expiration) {
  72.             int cookieLen;
  73.             if(cookie  && (cookieLen = cookie->length()))
  74.             {
  75.                 
  76.                 char *lpszUrl = *url;
  77.                 size_t buflen = cookieLen + 16 + strlen(expiration);
  78.                 char *lpszCookie = new char[buflen];
  79.                 BOOL ok;
  80.                 for(char *nextPiece, *cookiePiece = strtok_s(*cookie, "; ", &nextPiece); 
  81.                     cookiePiece != NULL; cookiePiece = strtok_s(NULL, "; ", &nextPiece))
  82.                 {
  83.                     if (cookiePiece[0] && sprintf_s(lpszCookie, buflen, "%s; expires = %s", cookiePiece, expiration) > 0)
  84.                         ok = InternetSetCookie(lpszUrl, NULL, lpszCookie);
  85.                 }
  86.  
  87.                 
  88.                 delete [] lpszCookie;
  89.             } 
  90.             
  91.         }
  92.  
  93.         static void setAll(const DownloadInfo *downloadInfo, long offsetExpiration)
  94.         {
  95.             char expiration[64];
  96.             makeExpiration(expiration,64,offsetExpiration);
  97.             LinkInfo *links=downloadInfo->links;
  98.             for(int j=downloadInfo->linksCount; j-->0;)
  99.             {
  100.                 LinkInfo l=links[j];
  101.                 setCookie(&l.url,&l.cookie,expiration);
  102.             }
  103.         }
  104.     
  105.     
  106. };
  107.  
  108. class FGArray
  109. {
  110.     private:
  111.         VARIANT varStr,array;
  112.         SAFEARRAY *psa;
  113.         int elemCount;
  114.         long ix[1];
  115.         
  116.     
  117.         void init(int elemCount)
  118.         {
  119.             this->psa=createSafeArray(elemCount);
  120.             this->elemCount=elemCount;
  121.             ix[0]=0;
  122.             varStr.vt=VT_BSTR;
  123.         }
  124.  
  125.     public:
  126.  
  127.     static SAFEARRAY *createSafeArray(int elemCount) 
  128.         {
  129.             SAFEARRAYBOUND bounds [1]; 
  130.             bounds[0].cElements=elemCount;
  131.             bounds[0].lLbound=0;
  132.             return SafeArrayCreate(VT_VARIANT, 1, bounds);
  133.         }
  134.  
  135.  
  136.         FGArray(int elemCount)
  137.         {
  138.             init(elemCount);
  139.         }
  140.         
  141.         FGArray(const DownloadInfo *downloadInfo)
  142.         {        
  143.             init(downloadInfo->linksCount * 2 + 1); // linksCount * 2 (url,info)
  144.             addString(downloadInfo->referer);
  145.             addLinks(downloadInfo);
  146.             
  147.         }
  148.         
  149.         void addLinks(const DownloadInfo *downloadInfo) {
  150.             LinkInfo *links=downloadInfo->links;
  151.             for (int j=0, linksCount=downloadInfo->linksCount; j < linksCount ; j++) 
  152.             {
  153.                 LinkInfo l=links[j];
  154.                 addString(l.url);
  155.                 addString(l.comment);
  156.             }
  157.         }
  158.  
  159.         BOOL addString(bstr_t s) 
  160.         {
  161.             if(ix[0]<elemCount) 
  162.             {
  163.                 varStr.bstrVal=s; 
  164.                 SafeArrayPutElement(psa, ix, &varStr); 
  165.                 ix[0]++;
  166.                 return TRUE;
  167.             }
  168.             return FALSE;
  169.         }
  170.  
  171.         BOOL putString(int idx, bstr_t s)
  172.         {
  173.             setIdx(idx);
  174.             return addString(s);
  175.         }
  176.         
  177.         void setIdx(int idx)
  178.         {
  179.             ix[0]=idx;
  180.         }
  181.         
  182.         VARIANT *asVariant(VARIANT *array)
  183.         {
  184.             array->vt = VT_ARRAY | VT_VARIANT | VT_BYREF; // VBScript Array
  185.             array->pparray=&psa;
  186.             return array;
  187.         }
  188.  
  189.         VARIANT *asVariant()
  190.         {
  191.             return asVariant(&this->array);
  192.         }
  193.  
  194.         ~FGArray()
  195.         {
  196.             SafeArrayDestroy(psa);
  197.         }
  198.     
  199. };
  200.  
  201.  
  202. class FGCOMGuard
  203. {
  204. private:
  205.     static FGCOMGuard *instance;
  206.     static int refCount;
  207.  
  208.     FGCOMGuard()
  209.     {
  210.         CoInitialize(NULL);
  211.     }
  212.     ~FGCOMGuard()
  213.     {
  214.         CoUninitialize();
  215.     }
  216. public:
  217.     
  218.     static void addClient()
  219.     {
  220.         if(!instance) instance=new FGCOMGuard();
  221.         refCount++;
  222.     }
  223.     
  224.     static void removeClient()
  225.     {
  226.         if(--refCount<0 || !instance) throw "FGGuard Illegal state";
  227.         if(refCount == 0)
  228.         {
  229.             delete instance;
  230.         }
  231.         
  232.     }
  233.  
  234.  
  235. };
  236.  
  237.  
  238. // [START DOWNLOAD MANAGER SUPPORT CLASSES]
  239.  
  240. class DMSupport
  241. {
  242.  
  243. protected:
  244.     
  245.     static char *findProgram(HKEY baseKey, char *leafPath, char *leafName = "") {
  246.         long res;
  247.         HKEY hk;
  248.         char *exe=NULL;
  249.         
  250.         if( (res=RegOpenKeyEx(baseKey,leafPath,0,KEY_QUERY_VALUE,&hk))==ERROR_SUCCESS)
  251.         {
  252.             char *exepath = leafName;
  253.             long pathLen=0;    
  254.             if((res=RegQueryValueEx(hk,exepath,0,NULL,NULL,(LPDWORD)&pathLen))==ERROR_SUCCESS) 
  255.             {
  256.                 exe=new char[pathLen+1];
  257.                 BOOL fileExists=FALSE;
  258.                 if((res=RegQueryValueEx(hk,exepath,0,NULL,(LPBYTE)exe,(LPDWORD)&pathLen))==ERROR_SUCCESS)
  259.                 {
  260.                     struct stat statbuf;
  261.                     fileExists=!stat(exe,&statbuf);  
  262.                 }
  263.                 if(!fileExists) {
  264.                     delete exe;
  265.                     exe=NULL;
  266.                 }
  267.             }
  268.             RegCloseKey(hk);    
  269.         }
  270.         
  271.         return exe;
  272.         
  273.     }
  274.  
  275.     static BOOL createProcess(char *commandLine, PROCESS_INFORMATION *pi) {
  276.         STARTUPINFO si;
  277.         ZeroMemory( &si, sizeof(si) );
  278.         si.cb = sizeof(si);
  279.         
  280.         PROCESS_INFORMATION *mypi=NULL;
  281.         if(!pi) {
  282.             pi=mypi=new PROCESS_INFORMATION;
  283.         }
  284.         ZeroMemory(pi, sizeof(pi) );
  285.  
  286.         
  287.         BOOL ret=CreateProcess( NULL, 
  288.                 commandLine, // Command line. 
  289.                 NULL,             // Process handle not inheritable. 
  290.                 NULL,             // Thread handle not inheritable. 
  291.                 FALSE,            // Set handle inheritance to FALSE. 
  292.                 0,                // No creation flags. 
  293.                 NULL,             // Use parent's environment block. 
  294.                 NULL,             // Use parent's starting directory. 
  295.                 &si,              // Pointer to STARTUPINFO structure.
  296.                 pi )             // Pointer to PROCESS_INFORMATION structure.
  297.             ;
  298.         
  299.         if(mypi) {
  300.             closeProcess(mypi);
  301.             delete mypi;
  302.         }
  303.         
  304.         return ret;
  305.     }
  306.  
  307.     static void closeProcess(PROCESS_INFORMATION *pi) {
  308.         CloseHandle( pi->hProcess );
  309.         CloseHandle( pi->hThread );
  310.     }
  311.  
  312.  
  313. public:
  314.     
  315.     
  316.     virtual void check(void) = 0;
  317.     virtual void dispatch(const DownloadInfo *downloadInfo) = 0;
  318.     virtual const char *getName(void) = 0;
  319.  
  320. };
  321.  
  322. #define COMCALL(Call) if(hr=FAILED(Call)) throw _com_error(hr)
  323. #define HELPER(name) FGCOMHelper name(this->getProgId())
  324. class FGCOMHelper
  325. {
  326. private:
  327.     HRESULT hr;
  328.     CComDispatchDriver  comObj;
  329.     CComPtr<IDispatch>  lpTDispatch;
  330.     
  331.     VARIANT tmpVarStr;
  332.  
  333.     const char *className;
  334.  
  335.     
  336.     void prepareCOMObj() {
  337.         
  338.         COMCALL(lpTDispatch.CoCreateInstance(_bstr_t(className),NULL));
  339.         comObj=lpTDispatch;
  340.     }
  341.  
  342. public:
  343.     
  344.     void getMemberID(char *memberName, DISPID *dispid) {
  345.         USES_CONVERSION;
  346.         OLECHAR FAR* oleMember=A2OLE(memberName);
  347.         COMCALL(comObj->GetIDsOfNames(IID_NULL, &oleMember, 1, LOCALE_SYSTEM_DEFAULT, dispid));
  348.     }
  349.  
  350.     void invoke(DISPID *dispid, VARIANT *parms, const unsigned int parmsCount) {    
  351.         COMCALL(comObj.InvokeN(*dispid,parms,parmsCount,NULL));
  352.     }
  353.     
  354.     void invoke(DISPID *dispid) {    
  355.         COMCALL(comObj.Invoke0(*dispid,NULL));
  356.     }
  357.  
  358.     void invoke(char *memberName, VARIANT *parms, const unsigned int parmsCount) {
  359.         DISPID dispid;
  360.         getMemberID(memberName,&dispid);
  361.         invoke(&dispid,parms,parmsCount);
  362.     }
  363.  
  364.     void invoke(char *memberName, _bstr_t parm) {
  365.         tmpVarStr.bstrVal=parm;
  366.         invoke(memberName,&tmpVarStr,1);
  367.     }
  368.     
  369.     void invoke(char *memberName) {
  370.         invoke(memberName,&tmpVarStr,0);
  371.     }
  372.  
  373.     IDispatch *getIDispatch() {
  374.         return lpTDispatch;
  375.     }
  376.     void set(char *propertyName, VARIANT *val) {
  377.         USES_CONVERSION;
  378.         LPCOLESTR oleMember=A2COLE(propertyName);
  379.         COMCALL(comObj.PutPropertyByName(oleMember, val));
  380.     }
  381.     
  382.     void set(char *propertyName, bstr_t val) {
  383.         tmpVarStr.bstrVal=val;
  384.         set(propertyName,&tmpVarStr);
  385.     }
  386.  
  387.     void get(char *propertyName, VARIANT *val, UINT indexCount) {
  388.         DISPPARAMS dispparams = {indexCount==0?NULL:&(val[1]), NULL, indexCount, 0};
  389.         DISPID dispid;
  390.         getMemberID(propertyName,&dispid);
  391.         COMCALL(lpTDispatch->Invoke(dispid, IID_NULL,
  392.                 LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
  393.                 &dispparams, val, NULL, NULL));
  394.     }
  395.     
  396.     
  397.  
  398.  
  399.     FGCOMHelper(const char *className) {
  400.     
  401.         this->className=className;
  402.         prepareCOMObj();
  403.         tmpVarStr.vt=VT_BSTR;
  404.     }
  405.  
  406.     
  407.     ~FGCOMHelper()
  408.     {
  409.         
  410.     }
  411.  
  412. };
  413.  
  414.  
  415. class DMSupportCOM :
  416.     public DMSupport
  417. {
  418.  
  419. protected:
  420.     
  421.     
  422.     virtual const char * getProgId() = 0;
  423.     
  424. public:
  425.     
  426.     DMSupportCOM() {
  427.         FGCOMGuard::addClient();
  428.     }
  429.     void check() 
  430.     {
  431.         HRESULT hr;
  432.         CLSID clsid;
  433.         COMCALL(CLSIDFromProgID(_bstr_t(getProgId()),&clsid));
  434.     }
  435.     
  436.     
  437.  
  438.     virtual ~DMSupportCOM() {
  439.         FGCOMGuard::removeClient();
  440.     }
  441.     
  442. };
  443.  
  444.  
  445. class DMSDownloadAcceleratorPlus:
  446.     public DMSupportCOM
  447. {
  448. protected:
  449.     
  450.     const char * getProgId() 
  451.     { 
  452.         return "dapie.catcher"; 
  453.     }
  454.  
  455. public:
  456.     const char * getName() 
  457.     { 
  458.         return "Download Accelerator Plus";  
  459.     }
  460.     void dispatch(const DownloadInfo *downloadInfo);
  461.     
  462. };
  463.